package com.javakc.platform.configurations.security;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @author zhouhonggang
 * @version 1.0.0
 * @project springboot-development-platform
 * @datetime 2022-04-12 18:00
 * @description: [前后端分离设置后端跨域访问]
 */
@Configuration
public class WebCorsConfiguration implements WebMvcConfigurer {

    @Override
    public void addCorsMappings(CorsRegistry corsRegistry){
        /*
         * 所有请求都允许跨域，使用这种配置就不需要设置拦截规则
         * 应用上线前需更换正式域名及headers
         */
        corsRegistry.addMapping("/**")
                .allowCredentials(true)
                .allowedOriginPatterns("*")
                .allowedMethods("POST", "GET", "PUT", "OPTIONS", "DELETE")
                .allowedHeaders("*")
                .maxAge(3600);
    }

}
